'''

完成所有数据统计的操作
'''
from rest_framework.views import APIView

from goods.models import GoodsVisitCount
from users.models import User
from rest_framework.response import Response
from rest_framework.permissions import IsAdminUser
from datetime import date, timedelta


class UserTotalCountView(APIView):
    '''
    用户总数统计
    '''
    # 指定权限
    permission_classes = [IsAdminUser]
    def get(self,request):
        # 1. 查询用户表，获取用户总数
        count = User.objects.all().count()
        # 2.返回用户总数
        return Response({
            'count':count
        })


class UserDayCountView(APIView):
    '''
    当天注册用户数统计
    '''
    # 指定权限
    permission_classes = [IsAdminUser]
    def get(self,request):
        # 1. 获取当天日期
        now_date = date.today()
        # 2. 根据当天日期查询用户表，获取当天注册用户数
        count = User.objects.filter(date_joined__gte=now_date).count()
        # 3.返回用户总数
        return Response({
            'count':count
        })


class UserDayCountActiveView(APIView):
    '''
    当天登录用户数统计
    '''
    # 指定权限
    permission_classes = [IsAdminUser]
    def get(self,request):
        # 1. 获取当天日期
        now_date = date.today()
        # 2. 根据当天日期查询用户表，获取当天登录用户数
        count = User.objects.filter(last_login__gte=now_date).count()
        # 3.返回用户总数
        return Response({
            'count':count
        })


class UserDayCountordersView(APIView):
    '''
    当天下单用户数统计
    '''
    # 指定权限
    permission_classes = [IsAdminUser]
    def get(self,request):
        # 1. 获取当天日期
        now_date = date.today()
        # 2. 根据当天日期查询用户表，获取当天下单用户数，过滤关联对象查询
        count = User.objects.filter(orders__create_time__gte=now_date).count()
        # 3.返回用户总数
        return Response({
            'count':count
        })


class UserMonthCountView(APIView):
    '''
    获取一个月范围内的新增用户
    '''
    # 指定权限
    permission_classes = [IsAdminUser]
    def get(self, request):
        # 1.获取当天日期
        now_date = date.today()
        # 2.获取一个月之前的日期
        start_date = now_date - timedelta(30)
        # 3.从30天之前的日期开始，循环遍历获取每天的注册用户数量
        data_list = []
        for i in range(31):
            # 指定当天日期
            first_date = start_date + timedelta(i)
            # 指定下一天日期
            next_date = start_date + timedelta(i+1)
            count = User.objects.filter(date_joined__gte=first_date,
                                        date_joined__lt=next_date).count()
            data_list.append({
                'count':count,
                'date':first_date
            })
        # 4.返回每天的注册数量和当天的日期
        return Response(data_list)


class GoodsTypeCountView(APIView):
    '''
    获取商品分类访问量
    '''
    # 指定权限
    permission_classes = [IsAdminUser]
    def get(self, request):
        # 1.获取当天日期
        now_date = date.today()
        # 2. 根据日期查询访问数据
        goodsvisit = GoodsVisitCount.objects.filter(date__gte=now_date)
        data_list = []
        for goods in goodsvisit:
            data_list.append({
                'count':goods.count,
                'category':goods.category.name
            })
        # 4.返回每天的注册数量和当天的日期
        return Response(data_list)